package com.lhn.tankGame4;

/**
 * @author lhn
 * @date 2024-07-28 14:30
 */

public class Shoot implements Runnable{
    private int x;
    private int y;
    private int direction = 0;
    private int speed = 10;
    boolean isLive = true;  //子弹是否存活

    public Shoot(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    public Shoot(int x, int y, int direction, int speed) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.speed = speed;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    @Override
    public String toString() {
        return "shoot{" +
                "x=" + x +
                ", y=" + y +
                ", direction=" + direction +
                ", speed=" + speed +
                ", isLive=" + isLive +
                '}';
    }

    @Override
    public void run() {

        System.out.println("射击-当前线程名：" + Thread.currentThread().getName());
        while(true) {
            try {
                Thread.sleep(50);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            switch (
                    direction
            ) {
                case 0:
                    y = y - speed;
                    break;
                case 1:
                    x = x + speed;
                    break;
                case 2:
                    y = y + speed;
                    break;
                case 3:
                    x = x - speed;
                    break;
            }
//            System.out.println("(" + x + "," + y + ")");

            //子弹超出屏幕边框 and 子弹碰到敌人的坦克

            if ( !(x >= 0 && x <= 1000 && y  >= 0 && y<=750 && isLive) ) {
                isLive = false;
                break;
            }
        }
    }

    public static void main(String[] args) {
        Shoot shoot = new Shoot(0, 0, 1);
        Thread thread = new Thread(shoot);
        thread.start();
    }
}
